package jml.defaultimple.web;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletContext;

import jml.def.Filter;
import jml.def.Label;
import jml.def.Library;
import jml.def.Title;
import jml.defaultimple.util.JMLPluginManager;

public class AsynchHtml
{
	public static String getHtml(ServletContext application, String filterName,String labelName)
	{
		Library library = (Library)application.getAttribute("library");
		
		Filter filter = library.getFilter(filterName); 
		Label label = filter.getLabel(labelName);
		
		String ret = "<div class='secccionContent'>";  
		ret+=showTitles(application, filter.getName(), label.getName());
		
		List<String> sublabels = label.getSublabels();
		sublabels = sublabels!=null?sublabels:new ArrayList<String>();
		
		for(String sub:sublabels)	
		{
			Label xlabel = filter.getLabel(sub);	
			if( xlabel==null || xlabel.getTitles().size()==0 )
			{ 
				continue;
			}
			
			ret+="<div class='labelSeparator'>";
			ret+="<span class='label'>";
			ret+=xlabel.getName();
			ret+="</span>";
			ret+="</div>";

		ret+=showTitles(application, filter.getName(), xlabel.getName());
//		<jsp:include page="zShowTitles.jsp">
//			<jsp:param name="filterName" value="<%=filter.getName()%>" />
//			<jsp:param name="labelName" value="<%=xlabel.getName()%>" />		
//		</jsp:include>	
	}			
		ret+="<div style='clear:both;'></div>";
		ret+="</div>";	
		return ret;
	}
	
	public static String showTitles(ServletContext application,String filterName,String labelName)
	{
		try
		{
			WebFacade webFacade=(WebFacade)application.getAttribute("webFacade");
			Library library = (Library)application.getAttribute("library");			
					
			Filter filter = library.getFilter(filterName);
			Label label = filter.getLabel(labelName);	
	
			String ret="";
			
			List<Title> titles=label.getTitles();
			for(Title title:titles)
			{
				String tooltip = webFacade.getTooltip(title);
				
				String urlEncode=URLEncoder.encode(title.getPath(), "UTF-8");
				
				ret+="<div class='Album'>";
				ret+="<div class='thumb' onclick=\"playAlbum('"+urlEncode+"')\">";
				ret+="<div class='cover' style='position: relative;'>";
				ret+="<img src='image?path="+title.getPath()+"' alt='' title='"+title.getId()+"'>";  
				ret+="</div>";
				ret+="</div>";
	
//				if( webFacade.aplicarTooltip(coverFullName) )
//				{
					ret+="<div class='tooltip'>"+tooltip+"<div class='edit' title='Edit Descriptor' onclick=\"editAlbum('"+urlEncode+"');\"></div>" +
							"<div class='play' title='Play Album' onclick=\"playAlbum('"+urlEncode+"');\"></div>" +
							"<div class='add_playlist' title='Add to Playlist' onclick=\"addToPlayList('"+urlEncode+"');\"></div>" +
							"<div class='open_folder' title='Open Folder' onclick=\"exploreAlbum('"+urlEncode+"');\"></div>" +
							"<div class='slide_show' title='Slideshow' onclick=\"slideShowAlbum('"+urlEncode+"');\"></div>" +
							"</div>"; 			         
//				}
	
				ret+="</div>";			
			}
			
			return ret;
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
			throw new RuntimeException(ex);
		}
	}
}
